package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.IBaiduAiAuditService;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.PetOnlineAuditLog;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.PetOnlineAuditLogMapper;
import cn.itsource.pet.service.IPetService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;


    @Override
    @Transactional
    public void add(Pet pet) {
        //添加宠物信息
        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);

        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){
            //好像前端只有adoptNotice和intro，但是宠物详情id也是有的
            petDetailMapper.update(petDetail);
        }

    }

    @Override
    @Transactional
    public void delete(Long id) {
        //删除宠物信息
        super.delete(id);
        //删除宠物详情 - 根据宠物，不是根据id
        petDetailMapper.deleteByPetId(id);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.findById(id);
            //审核内容和图像
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            if(boo1 && boo2){
                //改状态
                petMapper.onsale(id,new Date());
                //添加审核日志
                log.setState(1);//审核成功
                log.setNote("审核成功");
                petOnlineAuditLogMapper.add(log);

            }else{
                //添加审核日志
                log.setState(0);//审核失败
                log.setNote("审核失败");
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMessage("上架失败");
            }
        }
        //1.上架审核【内容审核，图像审核】 - 每一个宠物
        //2.审核失败：状态不改 ，要添加审核日志 - 审核失败为0
        //3.审核成功：状态改为1 ，要添加审核日志 - 审核成功为1
        return new AjaxResult();
    }

    @Override
    public AjaxResult offsale(List<Long> ids) {
        petMapper.offsale(ids,new Date());
        return new AjaxResult();
    }

    @Override
    public Pet getById(Long id) {
        return petMapper.getById(id);
    }
}
